package com.uyayo.notepad.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CategoryNotFoundException;
import com.uyayo.framework.common.exception.ModifiedRecordNotExistException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.notepad.entity.Favorite;
import com.uyayo.notepad.entity.FavoriteCategory;
import com.uyayo.notepad.mapper.FavoriteCategoryMapper;
import com.uyayo.notepad.mapper.FavoriteMapper;
import com.uyayo.notepad.model.FavoriteCondition;
import com.uyayo.notepad.service.FavoriteService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("favoriteService")
public class FavoriteServiceImpl extends BaseServiceImpl<Favorite> implements FavoriteService {

	private final FavoriteMapper favoriteMapper;

	private final FavoriteCategoryMapper favoriteCategoryMapper;

	public FavoriteServiceImpl(FavoriteMapper favoriteMapper, FavoriteCategoryMapper favoriteCategoryMapper) {
		this.favoriteMapper = favoriteMapper;
		this.favoriteCategoryMapper = favoriteCategoryMapper;
	}

	@Override
	public List<Favorite> selectFavorite(FavoriteCondition favoriteCondition) {
		Example example = new Example(Favorite.class);
		example.createCriteria();
		return favoriteMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<Favorite> selectFavoriteByPaging(PageBean<Favorite> pageBean,FavoriteCondition favoriteCondition) {

		List<Favorite> userList;
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		
		Example example = new Example(Favorite.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(favoriteCondition.getTitle())){
			criteria.andLike("title","%"+favoriteCondition.getTitle()+"%");
		}
		if(Objects.nonNull(favoriteCondition.getMemberId())){
			criteria.andEqualTo("memberId",favoriteCondition.getMemberId());
		}
		if(Objects.nonNull(favoriteCondition.getFavoriteCategoryId())){
			criteria.andEqualTo("favoriteCategoryId",favoriteCondition.getFavoriteCategoryId());
		}
		if(favoriteCondition.getBeginCreateTime()!=null || favoriteCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", favoriteCondition.getBeginCreateTime(), favoriteCondition.getEndCreateTime());
		}
		userList = favoriteMapper.selectByExample(example);

		//取记录总条数
		PageInfo<Favorite> pageInfo = new PageInfo<>(userList);
		pageBean.setTotalCount(pageInfo.getTotal());
		pageBean.setList(userList);
		return pageBean;
	}
	

	@Override
	public int saveFavorite(Favorite favorite) throws CategoryNotFoundException {
		Long favoriteCategryId = favorite.getFavoriteCategoryId();
		FavoriteCategory favoriteCategory = favoriteCategoryMapper.selectByPrimaryKey(favoriteCategryId);
		if(favoriteCategory==null){
			throw new CategoryNotFoundException("参数异常，分类不存在，请刷新重试或联系管理员！");
		}
		Integer contentCount = favoriteCategory.getContentCount();
		favoriteCategory.setContentCount(contentCount+1);
		favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategory);
		return favoriteMapper.insertSelective(favorite);
	}

	@Override
	public int updateFavorite(Favorite favorite) throws CategoryNotFoundException, ModifiedRecordNotExistException {
		Long favoriteCategryId = favorite.getFavoriteCategoryId();
		FavoriteCategory favoriteCategory = favoriteCategoryMapper.selectByPrimaryKey(favoriteCategryId);
		if(favoriteCategory==null){
			throw new CategoryNotFoundException("参数异常，分类不存在，请刷新重试或联系管理员！");
		}

		Long favoriteId = favorite.getId();
		Favorite favoriteOld = favoriteMapper.selectByPrimaryKey(favoriteId);
		if(favoriteOld==null){
			throw new ModifiedRecordNotExistException("参数异常，要修改的记录不存在，请刷新重试或联系管理员！");
		}
		Long favoriteCategryIdOld = favoriteOld.getFavoriteCategoryId();

		if(!Objects.equals(favoriteCategryId,favoriteCategryIdOld)){
			Integer contentCount = favoriteCategory.getContentCount();
			favoriteCategory.setContentCount(contentCount+1);
			favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategory);

			FavoriteCategory favoriteCategoryOld = favoriteCategoryMapper.selectByPrimaryKey(favoriteCategryIdOld);
			if(favoriteCategoryOld!=null && favoriteCategoryOld.getContentCount()>0){
				Integer contentCountOld = favoriteCategoryOld.getContentCount();
				favoriteCategoryOld.setId(favoriteCategryIdOld);
				favoriteCategoryOld.setContentCount(contentCountOld-1);
				favoriteCategoryMapper.updateByPrimaryKeySelective(favoriteCategoryOld);
			}
		}
		return favoriteMapper.updateByPrimaryKeySelective(favorite);
	}
}
